package com.fr.report.web.ui.impl.page;

import com.fr.base.Style;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.Report;
import com.fr.report.WorkSheet;
import com.fr.report.core.ReportUtils;
import com.fr.report.js.JavaScriptImpl;
import com.fr.report.web.event.Listener;
import com.fr.report.web.ui.Composite;
import com.fr.report.web.ui.Label;
import com.fr.report.web.ui.ToolBarNumberEditor;
import com.fr.report.web.ui.WebContentUtils;
import com.fr.web.Repository;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.ShowWorkBookPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PageNavi extends Composite
{
  public Report getReport(Repository paramRepository)
  {
    WorkSheet localWorkSheet = new WorkSheet();
    localWorkSheet.setHeader(0, null);
    localWorkSheet.setFooter(0, null);
    int i = 40;
    if (ReportUtils.hasLayerReport4Template(paramRepository.getSessionIDInfor().getContextBook()))
      i = 60;
    Style localStyle = Style.DEFAULT_STYLE.deriveHorizontalAlignment(0);
    DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(0, 0, 1, 1, new Integer(0));
    localDefaultCellElement1.setWidget(new PageNumberEditor(i));
    localDefaultCellElement1.setStyle(localStyle);
    DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(1, 0, 1, 1, new Integer(0));
    localDefaultCellElement2.setWidget(new PageNumberLabel(null));
    localDefaultCellElement2.setStyle(localStyle);
    localWorkSheet.addCellElement(localDefaultCellElement1);
    localWorkSheet.addCellElement(localDefaultCellElement2);
    localWorkSheet.setColumnWidth(0, i + 5);
    localWorkSheet.setColumnWidth(1, i);
    return localWorkSheet;
  }

  public boolean equals(Object paramObject)
  {
    if ((paramObject == null) || (!(paramObject instanceof PageNavi)))
      return false;
    return super.equals(paramObject);
  }

  private static class PageNumberLabel extends Label
  {
    private PageNumberLabel()
    {
    }

    public Listener[] createListeners(Repository paramRepository)
    {
      return new Listener[] { new Listener(WebContentUtils.getContentPanel(paramRepository), "afterload", new JavaScriptImpl(onContentPanelAfterLoad(paramRepository)), false), new Listener(WebContentUtils.getContentPanel(paramRepository), "tabchange", new JavaScriptImpl(onTabChange(paramRepository)), false) };
    }

    private String onTabChange(Repository paramRepository)
    {
      return TemplateUtils.render("if(${CP}.lgps[arguments[1]].reportTotalPage>0)this.setText(\"/\"+${CP}.lgps[arguments[1]].reportTotalPage); else this.setText(\"/\");", "CP", WebContentUtils.getContentPanel(paramRepository));
    }

    private String onContentPanelAfterLoad(Repository paramRepository)
    {
      return TemplateUtils.render("if(${CP}.rtype==='view'){if(${CP}.curLGP.reportTotalPage>0)this.setText(\"/\"+${CP}.curLGP.reportTotalPage); else this.setText(\"/\");}else{if(${CP}.reportTotalPage>0)this.setText(\"/\"+${CP}.reportTotalPage); else this.setText(\"/\");}", "CP", WebContentUtils.getContentPanel(paramRepository));
    }

    PageNumberLabel(PageNavi.1 param1)
    {
    }
  }

  private static class PageNumberEditor extends ToolBarNumberEditor
  {
    private int width;

    public PageNumberEditor(int paramInt)
    {
      setAllowDecimals(false);
      setAllowNegative(false);
      setDisabled(true);
      this.width = paramInt;
    }

    public JSONObject createJSONConfig(Object paramObject, Repository paramRepository)
      throws JSONException
    {
      return super.createJSONConfig(paramObject, paramRepository).put("style", "text-align:center;width:" + this.width);
    }

    public Listener[] createListeners(Repository paramRepository)
    {
      ArrayList localArrayList = new ArrayList();
      localArrayList.addAll(Arrays.asList(super.createListeners(paramRepository)));
      localArrayList.add(new Listener("afteredit", new JavaScriptImpl(TemplateUtils.render("if(e.keyCode == $.ui.keyCode.ENTER){${CP}.gotoPage(this.getValue() - 1 + 1)}", "CP", WebContentUtils.getContentPanel(paramRepository)))));
      if ("view".equals(paramRepository.getSessionIDInfor().getPolicy().panelType()))
        localArrayList.add(new Listener(WebContentUtils.getContentPanel(paramRepository), "tabchange", new JavaScriptImpl(onTabChange(paramRepository)), false));
      return ((Listener[])localArrayList.toArray(new Listener[localArrayList.size()]));
    }

    private String onTabChange(Repository paramRepository)
    {
      return TemplateUtils.render("this.setValue((${CP}.lgps[arguments[1]].currentPageIndex));", "CP", WebContentUtils.getContentPanel(paramRepository));
    }

    protected String onContentPanelAfterLoad(Repository paramRepository)
    {
      return super.onContentPanelAfterLoad(paramRepository) + ";this.$editorComponent.attr('disabled', false);" + TemplateUtils.render("if(${CP}.rtype==='view'){this.setValue((${CP}.curLGP.currentPageIndex));}else{this.setValue((${CP}.currentPageIndex));}", "CP", WebContentUtils.getContentPanel(paramRepository));
    }
  }
}